﻿package hibernate.xml44.test;


import hibernate.xml44.pojo.UserWebsite;
import hibernate.xml44.pojo.WebUser;
import hibernate.xml44.pojo.Website;
import hibernate.xml44.util.HibernateHandler;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
public class Cascade {
	public static void main(String[] args) {
//		cascadeOpt(); 
	}

	public static void cascadeOpt() {
		Session session = HibernateHandler.getCurrentSession();
		session.beginTransaction();
		/*一个用户注册2个网站*/
		WebUser user = new WebUser();
		Website website1 = new Website();
		website1.setName("www.jqb.com");
		Website website2 = new Website();
		website2.setName("www.dong.com");
		/*先保存2个网站*/
		session.save(website1);
		session.save(website2);
		UserWebsite userWebsite1 = new UserWebsite();
		UserWebsite userWebsite2 = new UserWebsite();
		userWebsite1.setWebUser(user);
		userWebsite1.setWebsite(website1);
		userWebsite2.setWebUser(user);
		userWebsite2.setWebsite(website2);
		Set<UserWebsite> userWebsite =new HashSet<UserWebsite>();
		userWebsite.add(userWebsite1);
		userWebsite.add(userWebsite2);
		user.setName("admin");
		user.setSno(1000);
		user.setUserWebsite(userWebsite);
		/*直接保存用户,因为设置了级联操作,所以会同时更新UserWebsite*/
		session.save(user);
		session.getTransaction().commit();
	}
}

